//
//  LZLabelTableViewCell.m
//  laziz_Merchant
//
//  Created by ZhaoDongBo on 2016/12/20.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZLabelTableViewCell.h"
#import "Masonry.h"

#import "LZCouponListModel.h"
#import "NSDate+TimeCategory.h"

@interface LZLabelTableViewCell()

@property (nonatomic, strong) UILabel * titleLabel;
@property (nonatomic, strong) UILabel * subLabel;
@property (nonatomic, strong) UIView * line;
@property (nonatomic, strong) UIImageView * accessoryImageView;

@end

@implementation LZLabelTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = UIColorFromRGB(0xffffff, 1);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self configView];
        [self addAutoLayoutToCell];
    }
    return self;
}

- (void)configView {
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.subLabel];
    [self.contentView addSubview:self.line];
    [self.contentView addSubview:self.accessoryImageView];
}

- (void)addAutoLayoutToCell {
    
    WS(weakSelf);
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.leading.mas_equalTo(10);
    }];
    
    [self.subLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.leading.mas_equalTo(weakSelf.titleLabel.mas_trailing).with.offset(10);
        make.trailing.mas_lessThanOrEqualTo(-10);
    }];
    
    [self.line makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(0.6);
    }];
    
    [self.accessoryImageView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.titleLabel.mas_centerY);
        make.trailing.mas_equalTo(-10);
        make.size.mas_equalTo(CGSizeMake(6, 12));
    }];
}

- (UILabel *)titleLabel {
    
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textColor = UIColorFromRGB(0x333333, 1);
    }
    
    return _titleLabel;
}

- (UILabel *)subLabel {
    
    if (_subLabel == nil) {
        _subLabel = [[UILabel alloc] init];
        _subLabel.font = [UIFont systemFontOfSize:15];
        _subLabel.textColor = UIColorFromRGB(0xcccccc, 1);
    }
    
    return _subLabel;
}

- (UIView *)line {
    
    if (_line == nil) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    }
    
    return _line;
}

- (UIImageView *)accessoryImageView {
    
    if (_accessoryImageView == nil) {
        _accessoryImageView = [[UIImageView alloc] init];
        if ([[LZLanguageManager shareInstance] getLanguageType] == LanguageType_Ar) {
            _accessoryImageView.image = [UIImage imageNamed:@"lz_cell_accessoryimage_ar"];
        } else {
            _accessoryImageView.image = [UIImage imageNamed:@"lz_cell_accessoryimage_en"];
        }
    }
    
    return _accessoryImageView;
}

- (void)setCouponListModel:(LZCouponListModel *)couponListModel {

    if (couponListModel) {
        
        _couponListModel = couponListModel;
        
        if (self.tag == 1) {
            self.titleLabel.text = LZLocalizedString(@"object_of_the_group_order", @"");
            self.accessoryImageView.hidden = NO;
            if (couponListModel.grouponFoods) {
                self.subLabel.text = couponListModel.grouponFoods;
                self.subLabel.textColor = UIColorFromRGB(0x333333, 1);
            } else {
                self.subLabel.text = LZLocalizedString(@"please_enter_an_object", @"");
                self.subLabel.textColor = UIColorFromRGB(0xcccccc, 1);
            }
        }
        else if (self.tag == 2) {
            self.titleLabel.text = LZLocalizedString(@"original_price", @"");
            self.accessoryImageView.hidden = YES;
            if (couponListModel.grouponBeforeCost) {
                self.subLabel.text = [NSString stringWithFormat:@"%0.3f/BHD", couponListModel.grouponBeforeCost];
                self.subLabel.textColor = UIColorFromRGB(0x333333, 1);
            }
            else {
                NSMutableAttributedString *str = [[NSMutableAttributedString alloc]
                                                  initWithString:[NSString stringWithFormat:@"??? /BHD"]];
                
                NSRange range1 = [[str string] rangeOfString:@"???"];
                [str addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0xcccccc, 1) range:range1];
                
                NSRange range2 = [[str string] rangeOfString:@"/BHD"];
                [str addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x333333, 1) range:range2];
                [self.subLabel setAttributedText:str];
            }
        }
        else if (self.tag == 5) {
            self.titleLabel.text = LZLocalizedString(@"mine_coupons_finsh_time", @"");
            self.accessoryImageView.hidden = NO;
            if (couponListModel.grouponEndTime) {
                self.subLabel.text = [NSDate dateStrFromCstampTime:couponListModel.grouponEndTime/1000 withDateFormat:@"dd-MM-yyyy"];
                self.subLabel.textColor = UIColorFromRGB(0x333333, 1);
            }
            else {
                self.subLabel.text = LZLocalizedString(@"select_group_deadline", @"");
                self.subLabel.textColor = UIColorFromRGB(0xcccccc, 1);
            }
        }
        else{
            self.titleLabel.text = LZLocalizedString(@"expiration_date", @"");
            self.accessoryImageView.hidden = NO;
            if (couponListModel.grouponDeadline) {
                self.subLabel.text = [NSDate dateStrFromCstampTime:couponListModel.grouponDeadline/1000 withDateFormat:@"dd-MM-yyyy"];
                self.subLabel.textColor = UIColorFromRGB(0x333333, 1);
            }
            else {
                self.subLabel.text = LZLocalizedString(@"select_expiration_date", @"");
                self.subLabel.textColor = UIColorFromRGB(0xcccccc, 1);
            }
        }
    }
}

@end
